package random.fox.p2p.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

import random.fox.p2p.R;
import random.fox.p2p.bean.greendao.Message;
import random.fox.p2p.ui.customview.imageLoader.CircleBitmapDisplayer;

/**
 * Created by acer on 2015/3/30.
 */
public class LikeRecycleAdapter extends RecyclerView.Adapter<LikeRecycleAdapter.ViewHolder> {
    protected ImageLoader imageLoader = ImageLoader.getInstance();
    DisplayImageOptions options;
    private List<Message> messages;
    private Context context;

    public LikeRecycleAdapter(List<Message> messages) {
        this.messages = messages;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        if(context==null)
            context = viewGroup.getContext();
        View view = View.inflate(context,
                R.layout.like_item_view, null);
        ViewHolder holder = new ViewHolder(view);
        // 使用DisplayImageOptions.Builder()创建DisplayImageOptions
        options = new DisplayImageOptions.Builder().showImageOnLoading(R.mipmap.ic_launcher)
                .showImageForEmptyUri(R.mipmap.ic_launcher)  // 设置图片Uri为空或是错误的时候显示的图片
                .showImageOnFail(R.mipmap.ic_launcher)       // 设置图片加载或解码过程中发生错误显示的图片
                .cacheInMemory(true)                        // 设置下载的图片是否缓存在内存中
                .cacheOnDisk(true)                          // 设置下载的图片是否缓存在SD卡中
                .displayer(new CircleBitmapDisplayer())  // 设置成圆角图片
                .build();
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int i) {
        Message message = messages.get(i);
        imageLoader.displayImage("http://123.57.78.179:3000"+message.getHead_url(), viewHolder.like_item_head, options);
        imageLoader.displayImage("http://123.57.78.179:3000"+message.getM_url(), viewHolder.theme_pic);

        String nickname = message.getNickname();
        if (nickname == null)
            nickname = "游客";
        viewHolder.like_nickname.setText(nickname);

        viewHolder.theme_title.setText("#"+message.getTheme()+"#");
        viewHolder.theme_content.setText(message.getContent());
    }

    @Override
    public int getItemCount() {
        return messages.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        public ImageView like_item_head , theme_pic ,like_delete;
        public TextView like_nickname , theme_title, theme_content;

        public ViewHolder(View itemView) {
            super(itemView);
            like_item_head = (ImageView) itemView.findViewById(R.id.like_item_head);
            theme_pic = (ImageView) itemView.findViewById(R.id.theme_pic);
            like_delete = (ImageView) itemView.findViewById(R.id.like_delete);
            like_nickname = (TextView) itemView.findViewById(R.id.like_nickname);
            theme_title = (TextView) itemView.findViewById(R.id.theme_title);
            theme_content = (TextView) itemView.findViewById(R.id.theme_content);
            like_delete.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            Toast.makeText(context, "like_delte", Toast.LENGTH_SHORT).show();
        }
    }

}
